package org.kfive.nova.thread;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

/**
 * Created by admin on 2017-04-14.
 */
public class ForkJoinThread {
    static class SubTask extends RecursiveAction{
        private int[] data;
        private int start;
        private int end;
        public SubTask(int [] data,int start,int end){
            this.data = data;
            this.start = start;
            this.end = end;
        }
        @Override
        protected void compute() {
            if(end - start>10){
                int mid = (start+end)/2;
                SubTask upTask = new SubTask(data,start,mid);
                SubTask downTask = new SubTask(data,mid,end);
                invokeAll(upTask,downTask);
            }else {
                for(int i = start;i<end;i++){
                    data[i] = data[i]+1;
                }
            }
        }
    }

    public static void main(String[] args) {
        int n = 26;
        int[] a = new int[n];
        for(int i=0; i<n; i++) {
            a[i] = i;
        }
        SubTask task = new SubTask(a,0,n);
        ForkJoinPool pool = new ForkJoinPool();
        pool.invoke(task);
        for(int i=0; i<n; i++) {
            System.out.print(a[i]+" ");
        }
    }
}
